{extend name="common/base"/}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
    {if condition="$detail eq 0"}
    <form class="layui-form gg-form-bar border-t border-x">
        <div class="layui-input-inline" style="width: 160px;margin-right: 20px;">
            <input type="text" name="date" placeholder="请选择报表周期" class="layui-input" id="date" autocomplete="on">
        </div>
        <!--<div class="layui-input-inline" style="width: 160px;margin-right: 20px;">
            <input type="text" name="teacher" placeholder="教师名" class="layui-input" autocomplete="off"/>
        </div>-->
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">搜索</button>
    </form>
    <div class="layui-tab layui-tab-brief" lay-filter="kindTab">
        <ul class="layui-tab-title">
            {volist name="kinds" id="vo"}
            <li {eq name="$vo.name" value="$kind"} class="layui-this" {/eq}>{$vo.name}</li>
            {/volist}
        </ul>
        <div class="layui-tab-content">
            <table class="layui-hide" id="list" lay-filter="list"></table>
            <table class="layui-hide" id="summary" lay-filter="summary" style="margin-top: 50px;"></table>
        </div>
    </div>
    {else/}
    <table class="layui-hide" id="detail" lay-filter="detail"></table>
    {/if}
</div>
<script type="text/html" id="toolbarUser">
    <div class="layui-btn-container">
        <span class="layui-btn layui-btn-normal layui-btn-sm">送印人统计表</span>
        <span class="layui-btn layui-btn-normal layui-btn-sm" lay-event="export">导出</span>
    </div>
</script>
<script type="text/html" id="toolbarSummary">
    <div class="layui-btn-container">
        <span class="layui-btn layui-btn-normal layui-btn-sm">汇总表</span>
        <span class="layui-btn layui-btn-normal layui-btn-sm" lay-event="export">导出</span>
    </div>
</script>
<script type="text/html" id="toolbarDetail">
    <div class="layui-btn-container">
        <span class="layui-btn layui-btn-normal layui-btn-sm">文印人详情表</span>
        <span class="layui-btn layui-btn-normal layui-btn-sm" lay-event="export">导出</span>
        <span style="display: none;" user="{$user}" id="user"></span>
    </div>
</script>
<script type="text/html" id="bar">
    <div class="layui-btn-group">
        <span class="layui-btn layui-btn-xs" lay-event="view">详情</span>
    </div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];

    function gouguInit() {
        let table = layui.table, tool = layui.tool, form = layui.form, $ = layui.$;
        let date, teacher;

        layui.use('element', function(){
            let element = layui.element;

            // 一些事件触发
            element.on('tab(kindTab)', function(data){
                renderTable(this.innerHTML, date, teacher);
            });
        });

        layui.laydate.render({
            elem: '#date',
            type: 'date',
            range: true,
            format: 'yyyyMMdd'
        });

        //监听搜索提交
        form.on('submit(webform)', function (data) {
            layui.$('.header').show();
            date = data.field.date;
            teacher = data.field.teacher;

            renderTable("{$kind}", date, teacher);

            return false;
        });

        function renderTable(kind, d, t) {
            table.render({
                elem: '#list',
                toolbar: '#toolbarUser',
                defaultToolbar: [],
                title: '查询结果',
                url: '/admin/data/report', //数据接口
                where: {teacher: t, date: d, table: 'teacher', kind: kind},
                page: true, //开启分页
                limit: 10,
                cols: [
                    [{
                        field: 'name',
                        title: '送印人',
                        align: 'center',
                        width: 130
                    }, {
                        field: 'money',
                        title: '合计',
                        align: 'center',
                        width: 100
                    }, {
                        field: 'right',
                        fixed: 'right',
                        title: '详情',
                        toolbar: '#bar',
                        width: 132,
                        align: 'center'
                    }, {
                        field: 'ds',
                        hide: true
                    }, {
                        field: 'de',
                        hide: true
                    }]
                ]
            });

            table.render({
                elem: '#summary',
                toolbar: '#toolbarSummary',
                defaultToolbar: [],
                title: '汇总结果',
                url: '/admin/data/report', //数据接口
                where: {date: d, table: 'summary', kind: kind},
                page: true, //开启分页
                limit: 100,
                totalRow: true,
                cols: [
                    [{
                        field: 'op',
                        title: '文印类型',
                        align: 'center',
                        sort: true,
                        width: 130,
                        totalRowText: '合计'
                    }, {
                        field: 'paper',
                        title: '纸型',
                        align: 'center',
                        sort: true,
                        width: 100
                    }, {
                        field: 'double',
                        title: '单/双',
                        align: 'center',
                        sort: true,
                        width: 100
                    }, {
                        field: 'amount',
                        title: '数量',
                        align: 'center',
                        sort: true,
                        width: 100
                    }, {
                        field: 'price',
                        title: '价格',
                        align: 'center',
                        width: 100
                    }, {
                        field: 'money',
                        title: '金额',
                        align: 'center',
                        width: 100,
                        totalRow:true
                    }]
                ]
            });

            table.on('toolbar(list)', function (obj) {
                $.get("/admin/data/view?name=teacher&date=" + date + "&kind=" + kind, function (data) {
                    table.exportFile(['送印人', '合计'], JSON.parse(data), 'xls', kind+"-教师文印合计");
                });
            });

            table.on('toolbar(summary)', function (obj) {
                $.get("/admin/data/view?name=summary&date=" + date + "&kind=" + kind, function (data) {
                    table.exportFile(['文印类型', '纸型', '单/双', '数量', '价格', '金额'],
                        JSON.parse(data), 'xls', kind+"-文印合计");
                });
            });

            table.on('tool(list)', function (obj) {
                let data = obj.data;
                tool.side('/admin/data/report?detail=1&date=' + date + "&user=" + data.name + "&kind=" + kind);
            });
        }

        // 侧边栏渲染是重新加载一个弹出框，不能放在事件中
        table.render({
            elem: '#detail',
            toolbar: '#toolbarDetail',
            defaultToolbar: [],
            title: '文印人详情',
            url: '/admin/data/report', //数据接口
            where: {date: '{$date}', user: '{$user}', table: 'detail', kind:'{$kind}'},
            page: true, //开启分页
            limit: 100,
            totalRow: true,
            cols: [
                [{
                    field: 'user',
                    title: '送印人',
                    align: 'center',
                    width: 130,
                    totalRowText: '合计'
                }, {
                    field: 'date',
                    title: '日期',
                    align: 'center',
                    width: 130,
                }, {
                    field: 'op',
                    title: '文印类型',
                    align: 'center',
                    width: 130,
                }, {
                    field: 'paper',
                    title: '纸型',
                    align: 'center',
                    width: 100
                }, {
                    field: 'double',
                    title: '单/双',
                    align: 'center',
                    width: 100
                }, {
                    field: 'content',
                    title: '内容',
                    align: 'center',
                    width: 130,
                }, {
                    field: 'amount',
                    title: '数量',
                    align: 'center',
                    width: 100
                }, {
                    field: 'price',
                    title: '价格',
                    align: 'center',
                    width: 100
                }, {
                    field: 'sum',
                    title: '金额',
                    align: 'center',
                    width: 100,
                    totalRow:true
                }]
            ]
        });

        table.on('toolbar(detail)', function (obj) {
            $.get("/admin/data/view?name=detail&date={$date}&user={$user}&kind={$kind}", function (data) {
                table.exportFile(['送印人', '日期', '文印类型', '纸型', '单/双', '内容', '数量', '价格', '金额'],
                    JSON.parse(data), 'xls', "{$kind}-教师({$user})-文印详情");
            });
        });
    }
</script>
{/block}
<!-- /脚本 -->